<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="司机名称" prop="driverName">
        <el-input
          style="width: 240px"
          v-model="queryParams.driverName"
          placeholder="请输入司机名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="driverPhone">
        <el-input
          style="width: 240px"
          v-model="queryParams.driverPhone"
          placeholder="请输入司机手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="licensePlate">
        <el-input
          style="width: 240px"
          v-model="queryParams.licensePlate"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排队队列" prop="areaId">
        <el-select v-model="queryParams.areaId" placeholder="请选择类型" clearable style="width: 240px">
          <el-option v-for="area in areaList" :key="area.id" :label="area.name" :value="area.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="loadingType">
        <el-select v-model="queryParams.loadingType" placeholder="请选择类型" clearable style="width: 240px">
          <el-option v-for="loadingType in loadingTypeList" :key="loadingType.value" :label="loadingType.label" :value="loadingType.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable style="width: 240px">
          <el-option v-for="status in statusList" :key="status.value" :label="status.label" :value="status.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排队日期">
        <el-date-picker
          clearable
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <!-- <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['vqms:queue:reservation:delete']"
        >删除</el-button> -->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['vqms:queue:reservation:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="queueList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="当前排队序号" align="center" prop="queueIndex" :show-overflow-tooltip="true" width="120"/>
      <el-table-column label="排队队列" align="center" prop="areaName" :show-overflow-tooltip="true" width="150"/>
      <el-table-column label="司机名称" align="center" prop="driverName" :show-overflow-tooltip="true" width="150"/>
      <el-table-column label="司机电话" align="center" prop="driverPhone" :show-overflow-tooltip="true" width="150"/>
      <el-table-column label="车牌号" align="center" prop="licensePlate" :show-overflow-tooltip="true" width="120"/>
      <el-table-column label="库位" align="center" prop="storehouseLocationName" :show-overflow-tooltip="true" width="120"/>
      <el-table-column label="类型" align="center" prop="loadingType" width="120">
        <template v-slot="scope">
          <span v-if="scope.row.loadingType === 1">卸货</span>
          <span v-if="scope.row.loadingType === 2">自卸</span>
          <span v-if="scope.row.loadingType === 3">自提</span>
          <span v-if="scope.row.loadingType === 4">退货</span>
          <span v-if="scope.row.loadingType === 5">其他</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="150">
        <template v-slot="scope">
          <span v-if="scope.row.status === 1" style="color:red">排队中</span>
          <span v-if="scope.row.status === 2" style="color:#00BFFF">装卸货中</span>
          <span v-if="scope.row.status === 3" style="color:green">已完成</span>
        </template>
      </el-table-column>
      <el-table-column label="开始排队时间" align="center" prop="actualQueueTime" :show-overflow-tooltip="true"  width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.actualQueueTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始装卸货时间" align="center" prop="loadingTime" :show-overflow-tooltip="true"  width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.loadingTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="装卸货完成时间" align="center" prop="endTime" :show-overflow-tooltip="true" width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="150">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == 1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleTopping(scope.row)"
            v-hasPermi="['vqms:queue:reservation:update']"
          >置顶</el-button>
          <el-button
            v-if="scope.row.status == 2"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleNext(scope.row)"
            v-hasPermi="['vqms:queue:reservation:update']"
          >下一个</el-button>
          <el-button
            v-if="scope.row.status == 1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleChange(scope.row)"
            v-hasPermi="['vqms:queue:reservation:update']"
          >更换队列</el-button>
          <el-button
            v-if="scope.row.status == 3"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['vqms:queue:reservation:delete']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 更换队列对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24" >
            <el-form-item label="排队队列" prop="newAreaId">
              <el-select v-model="form.newAreaId" placeholder="请选择排队队列" clearable >
                <el-option v-for="area in areaList" :key="area.id" :label="area.name" :value="area.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listQueue,getQueue,addQueue,updateQueue,toppingQueue,nextQueue,changeQueue,delQueue } from "@/api/vqms/queue";
import { selectQueueAreaList} from "@/api/vqms/queueArea";

export default {
  //name: "Queue",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 排队表格数据
      queueList:[],
      // 排队队列集合
      areaList:[],
      // 类型
      loadingTypeList :[
        {
          label:"卸货",
          value:1
        },
        {
          label:"自卸",
          value:2
        },
        {
          label:"自提",
          value:3
        },
        {
          label:"退货",
          value:4
        },
        {
          label:"其他",
          value:5
        }
      ],
      // 状态
      statusList :[
        {
          label:"排队中",
          value:1
        },
        {
          label:"装卸货中",
          value:2
        },
        {
          label:"已完成",
          value:3
        },
      ],
       // 弹出层标题
       title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        noticeTitle: undefined,
        createBy: undefined,
        status: undefined
      },
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        area: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getQueAreaList();
  },
  methods: {
    /** 查询排队列表 */
    getList() {
      this.loading = true;
      listQueue(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.queueList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getQueAreaList(){
      selectQueueAreaList().then(response => {
        this.areaList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 置顶按钮操作 */
    handleTopping(row) {
      toppingQueue(row).then(response => {
        this.$modal.msgSuccess("置顶成功");
        this.getList();
      });
    },
    /** 下一个按钮操作 */
    handleNext(row) {
      nextQueue(row).then(response => {
        this.$modal.msgSuccess("下一个操作成功");
        this.getList();
      });
    },
    /** 更换队列按钮操作 */
    async handleChange(row) {
      this.reset();
      await getQueue(row.id).then(response => {
        this.form.id=row.id
        this.form.areaId=response.data.areaId;
        this.$set(this.form, "newAreaId", response.data.areaId);
        this.form.queueIndex=response.data.queueIndex
      });
      this.open = true;
      this.title = "更换队列";
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            changeQueue(this.form).then(response => {
              this.$modal.msgSuccess("更换队列成功");
              this.open = false;
              this.getList();
            });
          } 
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids
      this.$modal.confirm('是否确认删除？').then(function() {
        return delQueue(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('vqms/queue/export', {
        ...this.queryParams
      }, `排队记录.xlsx`)
    },
  }
};
</script>
